/**
 * 给定数组A，从中选数，要求任意选中数对之差小于等于K
 * 最多选多少个元素
 * 排序以后，挨个枚举。
 * 令Ai是所选最小的元素，看差值K以内一共能选多少个
 * 取最大即可
 */
#include <bits/stdc++.h>
using namespace std;
int getInt(){
	int sgn = 1;
	char ch = getchar();
	while( ch != '-' && ( ch < '0' || ch > '9' ) ) ch = getchar();
	if ( '-' == ch ) {sgn = 0;ch=getchar();}

	int ret = (int)(ch-'0');
	while( '0' <= (ch=getchar()) && ch <= '9' ) ret = ret * 10 + (int)(ch-'0');
	return sgn ? ret : -ret;
}

int A[210000];
int main(){
    //freopen("1.txt","r",stdin);
    int nofkase = getInt();
    while(nofkase--){
        int n = getInt();
        int k = getInt();
        for(int i=0;i<n;++i)A[i]=getInt();
        sort(A,A+n);
        int ans = 0;
        for(int i=0;i<n;++i){
            int* first = lower_bound(A,A+n,A[i]);
            int* second = lower_bound(A,A+n,A[i]+k+1);
            ans = max(ans, (int)(second-first));
        }
        printf("%d\n",ans);
    }
    return 0;
}
